package com.example.clinic.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyl.clinic.common.QueryPageParam;
import com.wyl.clinic.common.Result;
import com.example.clinic.entity.Doctor;
import com.example.clinic.mapper.DepMapper;
import com.example.clinic.mapper.DoctorMapper;
import com.example.clinic.service.DepService;
import com.example.clinic.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangyulong
 * @since 2024-01-30
 */
@RestController
@RequestMapping("/doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    @Autowired
    private DepService depService;
    @Autowired
    private DepMapper depMapper;
    @Autowired
    private DoctorMapper doctorMapper;


    //新增
    @PostMapping("/save")
    public Result save (@RequestBody Doctor doctor){
        return doctorService.save(doctor)?Result.suc():Result.fail();
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody Doctor doctor){
        return doctorService.updateById(doctor)?Result.suc():Result.fail();
    }
    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return doctorService.removeById(id)?Result.suc():Result.fail();
    }
    //
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();

        String name = (String)param.get("name");
//        String roleId = (String)param.get("roleId");

        Page<Doctor> page = new Page(1,2);
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Doctor> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(Doctor::getName,name);
            System.out.println(lambdaQueryWrapper);
        }



        IPage result = doctorService.page(page,lambdaQueryWrapper); //注意这里

        System.out.println("总记录"+result.getTotal());
        System.out.println(result.getRecords());
//        Dep dep = depMapper.selectById(Doctor.);




        return Result.suc(result.getRecords(),result.getTotal());
    }
    @GetMapping("/list")
    public Result list(){
        List list = depService.list();
        System.out.println(list);
        return Result.suc(list);
    }

    @PostMapping("/listC")
    public Result listC(@RequestBody QueryPageParam query){
        System.out.println(1);
        HashMap param = query.getParam();
        String ac = (String)param.get("ac");

        QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ac",ac);
        Doctor doctor = doctorMapper.selectOne(queryWrapper);
        System.out.println(doctor);

        System.out.println(ac);
        return Result.suc(doctor);
    }

}
